﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using System.Data;

namespace HRM
{
    public partial class frmGradeInfo : Form
    {
        bool isChange = false;
        int _GradeID;
        GradeInfo _GDInfo;
        DialogResult dlgResult;
        GradeClient wcfGD;
        RoleRightInfo listRole;
        public frmGradeInfo(int GradeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;
            LoadBranch();
            //LoadLanguage();
            _GradeID = GradeID;
            wcfGD = Service.Grade();
            _GDInfo = new GradeInfo();
            if (_GradeID > 0)
            {
                _GDInfo = wcfGD.Grade_GetInfo(_GradeID);
                Info2Form();
            }
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        private void LoadBranch()
        {
            BranchClient wcf = Service.Branch();
            DataTable dt;
            if (!Program.isAdmin && !Program.isViewAll)
            {
                dt = wcf.Branch_GetOne(Program.intBranchID).Tables[0];
            }
            else
            {
                dt = wcf.Branch_GetAll().Tables[0];
            }

            cboBranchCode.DisplayMember = "Branch_Name";
            cboBranchCode.ValueMember = "Branch_Code";
            cboBranchCode.DataSource = dt;

        }
        private void Info2Form()
        {
            txtGrade.Text = _GDInfo.Name;
            cboBranchCode.SelectedValue = _GDInfo.BranchCode;
            isChange = false;
        }

        void Form2Info()
        {
            _GDInfo.ID = _GradeID;
            _GDInfo.Name = txtGrade.Text;
            _GDInfo.BranchCode = Program.strBranchBHXH;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtGrade.Text.Trim().Length == 0)
            {
                txtGrade.Focus();
                MessageBox.Show("Nội dung không được để trống", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_GDInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _GDInfo = new GradeInfo();
            _GradeID = 0;
            txtGrade.Text = "";
            //cboBranchCode.SelectedIndex = 1;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_GradeID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfGD.Grade_Update(_GDInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _GradeID = _GDInfo.ID = wcfGD.Grade_Insert(_GDInfo);
                if (_GradeID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmGradeInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmGradeInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            wcfGD.Close();
            this.DialogResult = dlgResult;
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
